package com.egame.manage.biz.server.op;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.egame.ext.op.OperationActivityInfo;
import com.egame.manage.biz.server.ManageClientExt;

public class OperationActivityServiceDao extends BaseDao {
	
	public OperationActivityServiceDao()throws ExceptionCommonBase {
		super("egame_ext");
	}

	public int deleteAdvertisement(int activityId)throws ExceptionCommonBase{
		String sql = "update t_operation_activity set data_status = '0' where id = " + activityId;
		return this.executeUpdate(sql);
	}

	public List<Integer> listTerminalIdsByActivityId(int activityId) throws ExceptionCommonBase {
		String sql = "select terminal_id from t_terminal_activity" +
				" where activity_id = " + activityId;
		return this.getListInteger(sql);
	}

	public List<Integer> listActivityIdsByTerminalIds(List<Integer> terminalIds) throws ExceptionCommonBase {
		if(terminalIds != null && terminalIds.size()>0){
			String sql = "select DISTINCT activity_id from t_terminal_activity" +
					" where terminal_id  in ( " + Utils.toString(terminalIds) + ")";
			return this.getListInteger(sql);
		}
		return new ArrayList<Integer>();
	}
	
	public int addAdapt(int activityId, int terminalId) throws ExceptionCommonBase {
		if(!hasAdapt(activityId,terminalId)){
			String sql = "insert into t_terminal_activity(activity_id,terminal_id)values(" + activityId + "," + terminalId + ")";
			System.out.println(sql);
			return this.executeUpdate(sql);
		}
		return -1;
	}
	
	public boolean hasAdapt(int activityId,int terminalId)throws ExceptionCommonBase{
		String sql = "select count(*) from t_terminal_activity where terminal_id = " + terminalId + " and activity_id = " + activityId;
		return this.getInt(sql)>0;
	}
	
	public int deleteAdapt(int activityId, int terminalId) throws ExceptionCommonBase {
		String sql = "delete from t_terminal_activity where activity_id = " + activityId + " and terminal_id = " + terminalId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}
	
	public List<Integer> listActivity(OperationActivityInfo bean, List<Integer> terminalIds,List<Integer> position,int page,int rowsOfPage)throws ExceptionCommonBase{
		String sql = "select id from t_operation_activity where 1 = 1 ";
		if(bean != null){
			if(bean.getDataStatus()>=0){
				sql += " and data_status = " + bean.getDataStatus();						
			}
			if(bean.getRecordType() != null && bean.getRecordType().value() >0 ){
				sql += " and record_type = " + bean.getRecordType().value();
			}
			if(bean.getPosition()>0){
				sql += " and position = " + bean.getPosition();
			}	
			if(!Utils.stringIsNullOrEmpty(bean.getBeginString())){
				sql += " and  begin_time >= " + SqlUtils.QuataStr(bean.getBeginString());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getEndString())){
				sql += " and  end_time <= " + SqlUtils.QuataStr(bean.getEndString());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getDescription())){
				sql += " and description LIKE " + SqlUtils.QuataLikeStr(bean.getDescription());
			}
		}
		if(terminalIds != null && terminalIds.size()>0){
			sql += " and id in (" + Utils.toString(this.listActivityIdsByTerminalIds(terminalIds)) + ")";
		}
		if(position != null && position.size()>0){
			sql += " and position in (" + Utils.toString(position) + ")";
		}
		sql += "  ORDER BY sort_no desc ";
		sql += " limit " + page * rowsOfPage + "," + rowsOfPage;
		System.out.println(sql);
		return this.getListInteger(sql);
	}

	public int getActivityCount(OperationActivityInfo bean, List<Integer> terminalIds,List<Integer> position) throws ExceptionCommonBase {
		String sql = "select count(id) from t_operation_activity where 1 = 1 ";
		if(bean != null){
			if(bean.getDataStatus()>=0){
				sql += " and data_status = " + bean.getDataStatus();						
			}
			if(bean.getRecordType() != null && bean.getRecordType().value() >0 ){
				sql += " and record_type = " + bean.getRecordType().value();
			}
			if(bean.getPosition()>0){
				sql += " and position = " + bean.getPosition();
			}	
			if(!Utils.stringIsNullOrEmpty(bean.getBeginString())){
				sql += " and  begin_time >= " + SqlUtils.QuataStr(bean.getBeginString());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getEndString())){
				sql += " and  end_time <= " + SqlUtils.QuataStr(bean.getEndString());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getDescription())){
				sql += " and description LIKE " + SqlUtils.QuataLikeStr(bean.getDescription());
			}
		}
		if(terminalIds != null && terminalIds.size()>0){
			sql += " and id in (" + Utils.toString(this.listActivityIdsByTerminalIds(terminalIds)) + ")";
		}
		if(position != null && position.size()>0){
			sql += " and position in (" + Utils.toString(position) + ")";
		}
		System.out.println(sql);
		return this.getInt(sql);
	}
	
	public int upper(int appId, long loginUserId, int activityId) throws RemoteException {
		OperationActivityInfo bean = ManageClientExt.getInstance().getOperationActivityInfoById(appId, loginUserId, activityId);
		if(bean != null){
			OperationActivityInfo upper = this.getUpperOperationActivityInfo(appId, loginUserId, bean.getId(), bean.getRecordType().value(), bean.getPosition(), bean.getSortNo());
			if(upper != null){
				this.updateSortNo(appId, loginUserId, bean.getId(), upper.getSortNo());
				this.updateSortNo(appId, loginUserId, upper.getId(), bean.getSortNo());
				return 1;
			}
		}
		return -1;
	}
	
	public int lower(int appId, long loginUserId, int activityId) throws RemoteException {
		OperationActivityInfo bean = ManageClientExt.getInstance().getOperationActivityInfoById(appId, loginUserId, activityId);
		if(bean != null){
			OperationActivityInfo lower = this.getLowerOperationActivityInfo(appId, loginUserId, bean.getRecordType().value(), bean.getPosition(), bean.getSortNo());
			if(lower != null){
				this.updateSortNo(appId, loginUserId, bean.getId(), lower.getSortNo());
				this.updateSortNo(appId, loginUserId, lower.getId(), bean.getSortNo());
				return 1;
			}
		}
		return -1;
	}
	
	public OperationActivityInfo getUpperOperationActivityInfo(int appId, long loginUserId, int activityId,int type,int position,int sortNo)throws RemoteException{
		String sql = "select id from t_operation_activity" +
				" where record_type =  " + type +
				" and position =  " + position +
				" and sort_no >  " + sortNo +
				" and g_id = 1" +
				" ORDER BY sort_no asc LIMIT 1";
		int id = this.getInt(sql);
		return ManageClientExt.getInstance().getOperationActivityInfoById(appId, loginUserId, id);
	}

	public int getMaxSortNo(int type,int position)throws RemoteException{
		String sql = "select max(sort_no) from t_operation_activity" +
				" where record_type =  " + type +
				//" and position =  " + position +
				" and data_status = 1";
		System.out.println(sql);
		return this.getInt(sql);
	}

	public OperationActivityInfo getLowerOperationActivityInfo(int appId, long loginUserId, int type,int position,int sortNo)throws RemoteException{
		String sql = "select id from t_operation_activity" +
				" where record_type =  " + type +
				" and position =  " + position +
				" and sort_no <  " + sortNo +
				" ORDER BY sort_no desc LIMIT 1";
		int id = this.getInt(sql);
		return ManageClientExt.getInstance().getOperationActivityInfoById(appId, loginUserId, id);
	}
	
	public int updateSortNo(int appId, long loginUserId, int activityId,int sortNo)throws ExceptionCommonBase{
		String sql = "update t_operation_activity set sort_no = " + sortNo + " where id = " + activityId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}
	
	public int getActivityIdByPicId(long efsId) throws ExceptionCommonBase{
		String sql = "select id from t_operation_activity where pic_id = " + efsId;
		return this.getInt(sql);
	}
	
	public int updateBigPic(int sortno) throws ExceptionCommonBase{
		String sql = "update t_operation_activity set sort_no = " + sortno + "  where data_status = '1' and g_id >1";
		return this.executeUpdate(sql);
	}

	public int deleteBigPic() throws ExceptionCommonBase{
		String sql = "update t_operation_activity set data_status = '0'  where  g_id >1";
		return this.executeUpdate(sql);
	}
}
